package com.codeforces.edu98;

import java.util.Scanner;

public class C {
    Scanner in = new Scanner(System.in);
    void solve(){
        int t;
        t = in.nextInt();
        while (t-- > 0){
            char[] chars = in.next().toCharArray();
            int sum = 0;
            int al = 0;
            int bl = 0;
            for(char ch : chars) {
                if (ch == '(') {
                    al++;
                } else if (ch == '[') {
                    bl++;
                } else if (ch == ')') {
                    if (al > 0) {
                        al--;
                        sum++;
                    }
                } else {
                    if (bl > 0) {
                        bl--;
                        sum++;
                    }
                }
            }
            System.out.println(sum);

        }

    }
    public static void main(String[] args) {
        C s = new C();
        s.solve();
    }
}
